const jwt = require('../jwt')
// 用户登录
const userinfo = require('../connects/userInfo')
// 邀请码
const inviteCode = require('../connects/inviteCode')
// 用户基本信息
const userDetail = require('../connects/userDetail')
// 部门基本信息
const collegeInfo = require('../connects/collegeInfo')
// 帖子信息
const postInfo = require('../connects/postInfo')
// 帖子评论
const postReplies = require('../connects/postReplies')
// 私信
const msgInfo = require('../connects/msgInfo')
// 通知
const noticeInfo = require('../connects/noticeInfo')
var success = {
	meta: {
		msg: 'ok',
		status: 200
	}
}
var error = {
	meta: {
		msg: "no",
		status: 201
	}
}
var userService = {
	// 登录
	findUser: function (data, callback) {
		userinfo.find(data, function (res) {
			if (res.length) {
				var token = jwt.setJwt(data.userName)
				// console.log(data.userName)
				// console.log(res[0].token)
				callback({
					token: token,
					meta: {
						msg: '登录成功',
						status: 200
					}
				})
			} else {
				callback(error)
			}
		})
	},
	//注册时查询邀请码
	preInvite: function (data, callback) {
		inviteCode.find(data, function (res) {
			// console.log(res)
			if (res.length) {
				callback({
					meta: {
						msg: '存在',
						code: 1
					}
				})
			} else {
				callback({
					meta: {
						msg: '不存在',
						code: 0
					}
				})
			}
		})
	},
	//注册时查询昵称
	preRegister: function (data, callback) {
		userinfo.find(data, function (res) {
			// console.log(res)
			if (res.length) {
				callback({
					meta: {
						msg: '用户存在',
						code: 1
					}
				})
			} else {
				callback({
					meta: {
						msg: '用户不存在',
						code: 0
					}
				})
			}
		})
	},
	//注册
	register: function (data, callback) {
		userinfo.add(data, function (res) {
			if (res && data.level === 0) {
				noticeInfo.add(data, (resss) => {
					userDetail.add(data, function (ress) {
						if (ress) {
							callback(success)
						} else {
							callback(error)
						}
					})
				})
			} else if (res && data.level === 1) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 获取用户基本信息
	getuserInfo: function (data, callback) {
		userDetail.find(data, function (res) {
			if (res.length) {
				callback({
					data: res[0],
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			} else {
				callback(error)
			}
		})
	},
	//获取版块信息
	getcollege: function (callback) {
		collegeInfo.find({}, function (res) {
			callback({
				data: res,
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 修改版块信息
	editcollege: function (data, callback) {
		collegeInfo.update({
			collegeId: data.collegeId
		}, data.edit, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 删除版块
	deletecollege: function (data, callback) {
		collegeInfo.delete({ collegeId: data }, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	//获取全部帖子信息，不含内容,点赞
	getpostInfo: function (data, callback) {
		// console.log(data)
		postInfo.find(data, function (res) {
			if (res) {
				callback({
					data: res,
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			} else {
				callback(error)
			}
		})
	},
	// 获取单独帖子信息
	getappointPost: function (data, callback) {
		postInfo.find({
			_id: data
		}, function (res) {
			// console.log(res)
			if (res.length) {
				callback({
					data: res[0],
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			}
		})
	},
	// 发布帖子
	sendPost: function (data, callback) {
		postInfo.add(data, function (res) {
			if (res) {
				callback({
					meta: {
						msg: 'ok',
						status: 200
					}
				})

			}
		})
	},
	// 修改帖子的阅读量、点赞量
	updatePost: function (data, callback) {
		var option = data.msg
		// console.log(option)
		var revise
		var database
		function reviseFun(num, arrHandle) {
			if (arrHandle !== undefined) {
				return {
					[arrHandle]: {
						'praisePerson': data.praisePerson
					}
				}
			} else {
				return {
					$inc: {
						'readCount': num
					}
				}
			}
		}
		// 向真正revise赋值
		if (option === 'readCount') {//阅读
			revise = reviseFun(1)
		} else if (option === 'praiseCountAdd') {//点赞
			revise = reviseFun(1, '$addToSet')
		} else if (option === 'praiseCountRed') {//取消点赞
			revise = reviseFun(-1, '$pull')
		}
		// console.log(data.type)
		database = data.type === 'content' ? postInfo : postReplies
		// console.log(database)
		database.update({
			_id: data._id
		}, revise, function (res) {
			if (res) callback({
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 提交评论
	sendComment: function (data, callback) {
		postReplies.add(data, function (res) {
			if (res) {
				postInfo.update({
					_id: data.postId
				}, {
					$inc: {
						'commentCount': 1
					}
				}, function (comCouRes) {
					if (comCouRes) {
						callback({
							meta: {
								msg: 'ok',
								status: 200
							}
						})
					}
				})

			}
		})
	},
	//获取评论内容
	getcomments: function (data, callback) {
		// console.log(data)
		var factor = {
			postId: data.id
		}
		if (data.type === 'parise') {
			factor = {
				'_id': data.id
			}
		}
		postReplies.find(factor, function (res) {
			// console.log(res)
			callback({
				data: res,
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 获取帖子所有信息
	getpostAll: function (data, callback) {
		postInfo.find({
			publisher: data
		}, function (res) {
			callback({
				data: res,
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 查询私信空间
	getupRoom: function (data, callback) {
		msgInfo.find(data, function (res) {
			// console.log(res)
			callback({
				data: res,
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 创建私信空间
	setupRoom: function (data, callback) {
		msgInfo.add(data, function (res) {
			if (res) {
				callback({
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			}
		})
	},
	// 删除空间
	deleteRoom: function (data, callback) {
		msgInfo.delete({
			_id: data
		}, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 发送私信
	sendContent: function (data, callback) {
		// console.log(data)
		msgInfo.update({
			name: {
				$all: data.option
			}
		}, {
			$push: {
				msg: data.msg
			}
		}, {}, function (res) {
			// console.log(res)
			if (res) {
				callback({
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			} else {
				callback({
					meta: {
						msg: 'no',
						status: 201
					}
				})
			}
		})
	},
	// 获取私信
	getContent: function (data, callback) {
		if (data.option) {
			datas = {
				_id: data.id
			}
		} else {
			datas = {
				name: {
					$all: data.name
				}
			}
		}
		msgInfo.find(datas, function (res) {

			if (!data.option) {
				msgInfo.update(datas, {
					$set: {
						'msg.$[i].isread': 1
					}
				}, {
					'arrayFilters': [{ 'i.name': data.name[0] }]
				}, function (ress) {
					if (ress) {
						callback({
							data: res[0],
							meta: {
								msg: 'ok',
								status: 200
							}
						})
					} else {
						callback(error)
					}
				})
			} else {
				callback({
					data: res[0],
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			}

		})
	},
	// 修改资料
	editDetail: function (data, callback) {
		// console.log(data)
		userDetail.update({
			userName: data.username
		}, {
			$set: data.editData
		}, function (res) {
			if (res) {
				callback({
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			} else {
				callback({
					meta: {
						msg: 'no',
						status: 201
					}
				})
			}
		})
	},
	// 关注
	follow: function (data, callback) {
		var isAdd
		if (data.flag) {
			isAdd = '$addToSet'
		} else {
			isAdd = '$pull'
		}
		// console.log(isAdd)
		userDetail.update({
			userName: data.userName
		}, {
			[isAdd]: {
				fans: data.follow
			}
		}, function (res) {
			// console.log(res)
			userDetail.update({
				userName: data.follow
			}, {
				[isAdd]: {
					follow: data.userName
				}
			}, function (ress) {
				if (res && ress) {
					callback(success)
				} else {
					callback(error)
				}
			})
		})
	},
	//个人中心获取全部回复
	getpostReplies: function (data, callback) {
		postReplies.find(data, function (res) {
			// console.log(res)
			callback({
				data: res,
				meta: {
					msg: 'ok',
					status: 200
				}
			})
		})
	},
	// 开始后台数据
	// 查找用户
	getuser: function (data, callback) {
		if (data == '1') {
			userinfo.find({}, function (res) {
				callback({
					data: res,
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			})
		} else {
			userDetail.find({}, function (res) {
				callback({
					data: res,
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			})
		}
	},
	// 删除用户
	deleteuser: function (data, callback) {
		var datas = { userName: data }
		userinfo.delete(datas, function (res) {
			if (res) {
				userDetail.delete(datas, function (ress) {
					if (ress) {
						noticeInfo.delete(datas, function (resss) {
							if (resss) { callback(success) } else { callback(error) }
						})
					} else {
						callback(error)
					}
				})
			} else {
				callback(error)
			}
		})

	},
	// 删除帖子
	deletePost: function (data, callback) {
		postInfo.delete({ _id: data }, function (res) {
			if (res) {
				postReplies.delete({ postId: data }, function (ress) {
					if (ress) {
						callback(success)
					} else {
						callback(error)
					}
				})
			} else {
				callback(error)
			}
		})
	},
	// 修改帖子的类型
	typeChange: function (data, callback) {
		postInfo.update({ _id: data._id }, {
			$set: { type: data.type }
		}, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 获取积分的通知信息
	noticeInfo: function (data, callback) {
		noticeInfo.find({ userName: data }, function (res) {
			if (res) {
				callback({
					data: res[0],
					meta: {
						msg: 'ok',
						status: 200
					}
				})
			} else {
				callback(error)
			}
		})
	},
	// 管理员修改积分
	editnoticeInfo: function (data, callback) {
		// var login,post,comment,parise
		var editData = {}
		Object.keys(data.editData).forEach(item => {
			var integ = 'integ.'
			editData[integ + item] = data.editData[item]
		})
		userDetail.update({ userName: data.username }, {
			$set: editData
		}, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 普通用户修改积分
	// 每日登录+1
	// 发帖+1
	// 评论+1
	// 点赞+1
	editInteg: function (data, callback) {
		var msgType = data.type.split('.')[1]
		// console.log(data)
		userDetail.update({
			userName: data.name
		}, {
			$inc: {
				[data.type]: data.integ
			}
		}, function (res) {
			noticeInfo.update({ userName: data.name }, {
				'$addToSet': {
					[msgType]: data.notice
				}
			}, {}, function (ress) {
				if (ress) {
					callback(success)
				} else {
					callback(error)
				}
			})
		})
	},
	// 将评论/私信通知未读改为已读
	noticeRead: function (data, callback) {

		var type = data.type + '.$[i].isread'
		var factorObj = {}
		if (data.type === 'msg') {
			type = data.type + 'isread'
		} else {
			var factor = 'i.' + data.factor
			factorObj = {
				arrayFilters: [{
					[factor]: data.id
				}]
			}
		}
		// console.log(data)
		noticeInfo.update({
			userName: data.name
		}, {
			$set: {
				[type]: data.num
			}
		}, factorObj, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 管理员发通告
	adminNotice: function (data, callback) {
		console.log(data)
		noticeInfo.update({
			userName: 'admin'
		}, {
			$addToSet: {
				notice: data
			}
		}, {}, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	},
	// 创建邀请码
	inviteInsert: function (data, callback) {
		console.log(data)
		inviteCode.add(data, function (res) {
			if (res) {
				callback(success)
			} else {
				callback(error)
			}
		})
	}
}
module.exports = userService